package at.donalfonso.ui.pizzeria;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.HeadlessException;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
// import javax.swing.JSpinner;
// import javax.swing.SpinnerNumberModel;

import at.donalfonso.model.Bestellung;
import at.donalfonso.model.Gruppe;
import at.donalfonso.model.Tisch;

public class PizzeriaView extends JFrame {

	private static final long serialVersionUID = -899690079808761255L;

	private DefaultListModel<Gruppe> gruppeModel;
	private DefaultListModel<Tisch> tischModel;
	private DefaultListModel<Bestellung> bestellModel;
	@SuppressWarnings("unused")
	private DefaultListModel<Integer> platzModel;

	private JPanel gruppePanel;
	private JPanel tischPanel;
	private JPanel bestellPanel;
	private JPanel plaetzePanel;
	private JLabel plaetzeLabel;
	private JList<Integer> plaetzeList;
	private JScrollPane gruppeScrollPane;
	private JScrollPane tischScrollPane;
	private JScrollPane bestellScrollPane;
	
	public PizzeriaView(DefaultListModel<Gruppe> gruppeModel, DefaultListModel<Tisch> tischModel, 
			DefaultListModel<Bestellung> bestellModel, DefaultListModel<Integer> platzModel) throws HeadlessException {
		
		setSize(1000, 800);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.gruppeModel = gruppeModel;
		this.tischModel = tischModel;
		this.bestellModel = bestellModel;
		this.platzModel = platzModel;
		
		JPanel headerPanel = new JPanel();

		headerPanel.setLayout(new BorderLayout());
		setLayout(new BorderLayout());
		
		plaetzePanel = new JPanel();
		plaetzeLabel = new JLabel("Freie Plätze: ");
		plaetzeList = new JList<Integer>();
		plaetzeList.setModel(platzModel);
		plaetzeLabel.setLabelFor(plaetzeList);
		
		plaetzePanel.setLayout(new BorderLayout());
		plaetzePanel.add(plaetzeLabel, BorderLayout.WEST);
		plaetzePanel.add(plaetzeList, BorderLayout.CENTER);
		plaetzePanel.setBorder(BorderFactory.createTitledBorder("Info"));
		plaetzePanel.setPreferredSize(new Dimension(200, 70));
		
		JPanel infoPanel = new JPanel();
		infoPanel.setPreferredSize(new Dimension(MAXIMIZED_HORIZ, 350));
		infoPanel.setLayout(new BorderLayout());
		infoPanel.add(plaetzePanel, BorderLayout.WEST);
		infoPanel.add(getGruppePanel(), BorderLayout.CENTER);
		infoPanel.add(getTischPanel(), BorderLayout.EAST);
		
		headerPanel.add(infoPanel, BorderLayout.NORTH);
		headerPanel.add(getBestellPanel(), BorderLayout.SOUTH);
		
		getContentPane().add(headerPanel, BorderLayout.NORTH);

		setVisible(true);

	}

	private JPanel getGruppePanel() {
		
		if (gruppePanel == null) {
			gruppePanel = new JPanel();
			gruppePanel.setLayout(new BorderLayout());
			
			JList<Gruppe> gruppen = new JList<Gruppe>();
			gruppen.setModel(gruppeModel);
			
			gruppeScrollPane = new JScrollPane(gruppen);
			
			gruppePanel.add(gruppeScrollPane, BorderLayout.CENTER);
			gruppePanel.setBorder(BorderFactory.createTitledBorder("wartende Gruppen"));
			gruppePanel.setPreferredSize(new Dimension(280, 300));
		}
		return gruppePanel;
	}
	
	private JPanel getTischPanel() {
		
		if (tischPanel == null) {
			tischPanel = new JPanel();
			tischPanel.setLayout(new BorderLayout());
			
			JList<Tisch> tische = new JList<Tisch>();
			tische.setModel(tischModel);
			
			tischScrollPane = new JScrollPane(tische);
			
			tischPanel.add(tischScrollPane, BorderLayout.CENTER);
			tischPanel.setBorder(BorderFactory.createTitledBorder("Tische"));
			tischPanel.setPreferredSize(new Dimension(400, 300));
		}
		return tischPanel;
	}

	private JPanel getBestellPanel() {
		
		if (bestellPanel == null) {
			bestellPanel = new JPanel();
			bestellPanel.setLayout(new BorderLayout());
			
			JList<Bestellung> bestellungen = new JList<Bestellung>();
			bestellungen.setModel(bestellModel);
			
			bestellScrollPane = new JScrollPane(bestellungen);
			
			bestellPanel.add(bestellScrollPane, BorderLayout.CENTER);
			bestellPanel.setBorder(BorderFactory.createTitledBorder("Bestellungen"));
			bestellPanel.setPreferredSize(new Dimension(MAXIMIZED_HORIZ, 400));
		}
		return bestellPanel;
	}

}
